<?php
/**
 * CreateTime: 2022/11/2 15:53
 */

namespace App\Func;

use App\Models\Approval\OldDingTalkApprovalModel;
use App\Tools\DingTalk\DingTalkApprovalProcess;
use Illuminate\Support\Facades\DB;

class DingTalkApprovalFunc
{
    public static function dingTalkRollbackBySourceIdSourceType($sourceId,$sourceType)
	{
        $ptam			= new OldDingTalkApprovalModel();
        $appList		= $ptam->getTalkApprovalLogBySourceIdSourceTypeArr($sourceId,$sourceType)->toArray();
		if(count($appList)==0)
		{
            // 当前审批不存在
            return false;
        }
        try {
            DB::beginTransaction();
            $res = true;
            foreach($appList as $value){
                $ptam->editApproval($value['ding_talk_approval_process_id'],[
                    'ding_talk_approval_type_id' => 4
                ]);
                $res = DingTalkApprovalProcess::approvalTerminate($value['ding_talk_approval_process_id']);
            }
            if(!$res){
	
				DB::rollBack();
                return false;
            }
            DB::commit();
            return true;
        }catch (\Exception $exception){
            DB::rollBack();
            return false;
        }
    }
}